package com.utils;

import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.UiSelector;

import static android.os.SystemClock.sleep;

/**
 * 项目名称：yzf
 * 类描述：切换讯飞输入法
 * 创建人：Administrator
 * 创建时间：2019/5/17 9:49
 * 修改人：Administrator
 * 修改时间：2019/5/17 9:49
 * 修改备注：
 */
public class XunFeiConfig {

    private final UiDevice uiDevice;

    public XunFeiConfig(UiDevice mDevice) {
        this.uiDevice = mDevice;
        UiObject uiObject = mDevice.findObject(new UiSelector().text("讯飞输入法"));
        try {
            uiObject.click();
            config();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }

    }

    private void config() throws UiObjectNotFoundException {
        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_10) {
                LogToFile.d("====检测输入法====", "====输入法设置超时====");
                uiDevice.pressBack();
                return;
            }
            UiObject uiObject = uiDevice.findObject(new UiSelector().resourceId("com.iflytek.inputmethod:id/text_step2").text("去切换"));
            if (uiObject.exists()) {
                uiObject.click();
                break;
            }
            uiObject = null;
            sleep(1000);
        }
        while (true) {
            UiObject uiObject = uiDevice.findObject(new UiSelector().resourceId("android:id/text1").text("讯飞输入法"));
            if (uiObject.exists()) {
                LogToFile.d("====检测输入法====", "====输入法设置点击====");
                uiObject.click();
                sleep(1000);
                break;
            }
            uiObject = null;
            sleep(1000);
        }

        ChatClient.begintime = System.currentTimeMillis();//开始时间
        while (true) {
            long nowtime = System.currentTimeMillis();
            if ((nowtime - ChatClient.begintime) > ChatClient.overtime_10) {
                LogToFile.d("====检测输入法====", "====输入法设置超时====");
                uiDevice.pressBack();
                uiDevice.pressBack();
                return;
            }
            UiObject uiObject = uiDevice.findObject(new UiSelector().resourceId("com.iflytek.inputmethod:id/title_tv").text("26键拼音"));
            UiObject uiObject1 = uiDevice.findObject(new UiSelector().resourceId("com.iflytek.inputmethod:id/confirm_btn").text("确定"));
            if (uiObject.exists() && uiObject1.exists()) {
                LogToFile.d("====检测输入法====", "====输入法键盘选择====");
                uiObject.click();
                sleep(1000);
                uiObject1.click();
                sleep(4000);
                uiDevice.pressBack();
                uiDevice.pressBack();
                break;
            }
            uiObject = null;
            sleep(1000);
        }
    }
}
